package cn.broadsense.thrid.api;

import cn.broadsense.common.exception.ServiceException;
import cn.broadsense.thrid.constant.DaHuaApiUrlConstant;
import cn.broadsense.thrid.param.*;
import cn.broadsense.thrid.result.BasePageResult;
import cn.broadsense.thrid.result.SysCapGatewayChannelResult;
import cn.broadsense.thrid.result.SysCapGatewayConfigListResult;
import cn.broadsense.thrid.result.SysProxyConfigResult;
import cn.broadsense.thrid.utils.DaHuaHttpUtils;
import com.alibaba.fastjson2.JSONObject;
import org.springframework.stereotype.Component;

/**
 * 系统配置相关接口
 *
 * @author pengxin
 * @date 2022-10-10 16:34
 */
@Component
public class SysConfigRepository extends BaseRepository {


    /**
     * 代理服务列表
     *
     * @return
     */
    public SysProxyConfigResult proxyConfigList() {
        String token = getToken();
        BaseParam param = new BasePageParam() {
            @Override
            public void checkParam() {

            }

            @Override
            public Class getResultClass() {
                return SysProxyConfigResult.class;
            }
        };
        param.setUrl(url + DaHuaApiUrlConstant.SysConfig.PROXY_CONFIG);
        return DaHuaHttpUtils.getRest(token, param);
    }

    /**
     * 删除代理服务
     *
     * @param proxyId
     */
    public void delProxyConfig(String proxyId) {
        String token = getToken();
        BaseParam param = new BasePageParam() {
            @Override
            public void checkParam() {

            }

            @Override
            public Class getResultClass() {
                return String.class;
            }
        };
        param.setUrl(url + String.format(DaHuaApiUrlConstant.SysConfig.DEL_PROXY, proxyId));
        DaHuaHttpUtils.deleteRest(token, param);
    }

    /**
     * 新增代理服务
     *
     * @param param
     */
    public void addProxyConfig(SysProxyConfigParam param) {
        String token = getToken();
        param.setUrl(url + DaHuaApiUrlConstant.SysConfig.PROXY_CONFIG);
        DaHuaHttpUtils.postRest(token, param);
    }

    /**
     * 更新代理服务
     *
     * @param proxyId
     * @param param
     */
    public void updateProxyConfig(String proxyId, SysProxyConfigParam param) {
        String token = getToken();
        param.setUrl(url + String.format(DaHuaApiUrlConstant.SysConfig.DEL_PROXY, proxyId));
        DaHuaHttpUtils.putRest(token, param);
    }


    /**
     * 关联点位列表查询
     *
     * @param param
     * @return
     */
    public BasePageResult<SysCapGatewayChannelResult> capGatewayConfigChannel(SysCapGatewayChannelParam param) {
        String token = getToken();
        param.setUrl(url + DaHuaApiUrlConstant.SysConfig.CHANNEL_LIST);
        return DaHuaHttpUtils.postRest(token, param);
    }

    /**
     * 关联点位批量 绑定或解绑
     *
     * @param param
     */
    public void bindChannel(SysBindChannelParam param) {
        String token = getToken();
        param.setUrl(url + DaHuaApiUrlConstant.SysConfig.BIND_CHANNEL);
        DaHuaHttpUtils.postRest(token, param);
    }

    /**
     * 删除抓图网关配置
     *
     * @param capGatewayId 抓图网关id
     */
    public void delGatewayConfig(String capGatewayId) {
        String token = getToken();
        BaseParam param = new BasePageParam() {
            @Override
            public void checkParam() {

            }

            @Override
            public Class getResultClass() {
                return String.class;
            }
        };
        param.setUrl(url + String.format(DaHuaApiUrlConstant.SysConfig.CAP_GATEWAY_CONFIG_CHANGE, capGatewayId));
        DaHuaHttpUtils.deleteRest(token, param);
    }

    /**
     * 新增抓图网关
     *
     * @param param
     */
    public void addGatewayConfig(SysCapGatewayConfParam param) {
        param.setUrl(url + DaHuaApiUrlConstant.SysConfig.CAP_GATEWAY_CONFIG);
        DaHuaHttpUtils.postRest(getToken(), param);
    }

    /**
     * 修改抓图网关
     *
     * @param capGatewayId
     * @param param
     */
    public void updateGatewayConfig(String capGatewayId, SysCapGatewayConfParam param) {
        param.setUrl(url + String.format(DaHuaApiUrlConstant.SysConfig.CAP_GATEWAY_CONFIG_CHANGE, capGatewayId));
        DaHuaHttpUtils.putRest(getToken(), param);
    }


    /**
     * 抓图网关配置列表查询
     *
     * @param param
     * @return
     */
    public BasePageResult<SysCapGatewayConfigListResult> gatewayConfigList(SysQCapGatewayConfParam param) {
        param.setUrl(url + DaHuaApiUrlConstant.SysConfig.CAP_GATEWAY_CONFIG_LIST);
        return DaHuaHttpUtils.postRest(getToken(), param);
    }

    /**
     * 获取加密公钥
     *
     * @return
     */
    public String getPublicKey() {
        BaseParam param = new BasePageParam() {
            @Override
            public void checkParam() {

            }

            @Override
            public Class getResultClass() {
                return JSONObject.class;
            }
        };
        param.setUrl(url + DaHuaApiUrlConstant.SysConfig.PUBLIC_KEY);
        JSONObject rest = DaHuaHttpUtils.getRest(getToken(), param);
        if (rest.containsKey("publicKey")) {
            return rest.getString("publicKey");
        }
        throw new ServiceException("获取公钥失败");
    }

}

